package com.tsd.jxc.fin.service.impl;

import com.alibaba.fastjson.JSON;
import com.tsd.base.BaseCodeRuleConstants;
import com.tsd.base.service.BaseCodeRuleService;
import com.tsd.core.constants.Constants;
import com.tsd.core.service.GetDuplicatedObj;
import com.tsd.core.thread.ThreadPoolTaskUtil;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.PageBean;
import com.tsd.jxc.config.JxcCodeRuleConstants;
import com.tsd.jxc.fin.FinanceConstants;
import com.tsd.jxc.fin.dao.JxcFinancePaymentRequestDetailMapper;
import com.tsd.jxc.fin.dao.JxcFinancePaymentRequestMapper;
import com.tsd.jxc.fin.entity.JxcFinancePaymentRequest;
import com.tsd.jxc.fin.entity.JxcFinancePaymentRequestDetailExt;
import com.tsd.jxc.fin.entity.JxcFinancePaymentRequestExt;
import com.tsd.jxc.fin.service.JxcFinancePaymentRequestDetailService;
import com.tsd.jxc.fin.service.JxcFinancePaymentRequestService;
import com.tsd.jxc.fin.vo.PaymentRequestExportVo;
import com.tsd.jxc.order.dao.JxcOrderCutMoneyMapper;
import com.tsd.jxc.order.entity.JxcOrderCutMoneyExt;
import com.tsd.jxc.utils.JxcUtils;
import com.tsd.jxc.vo.JxcSettingVo;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysSettingExt;
import com.tsd.system.entity.SysUser;
import com.tsd.system.entity.SysUserExt;
import com.tsd.system.service.SysSettingService;
import com.tsd.system.service.SystemSettingService;
import com.tsd.system.vo.GlobalSetting;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * 描述：JxcFinancePaymentRequest 服务实现层
 *
 * @author Hillpool
 * @date 2023/02/09 18:00:21
 */
@Service
public class JxcFinancePaymentRequestServiceImpl extends BaseServiceImpl implements JxcFinancePaymentRequestService, GetDuplicatedObj {

    @Resource
    private JxcFinancePaymentRequestMapper jxcFinancePaymentRequestMapper;
    @Resource
    private JxcFinancePaymentRequestDetailMapper jxcFinancePaymentRequestDetailMapper;
    @Resource
    private JxcOrderCutMoneyMapper jxcOrderCutMoneyMapper;
    @Resource
    private BaseCodeRuleService baseCodeRuleService;
    @Resource
    private JxcFinancePaymentRequestDetailService jxcFinancePaymentRequestDetailService;
    @Resource
    private SystemSettingService systemSettingService;
    @Resource
    private SysSettingService sysSettingService;

    @Override
    public JxcFinancePaymentRequest getJxcFinancePaymentRequestById(Long id) throws Exception {
        return jxcFinancePaymentRequestMapper.selectByPrimaryKey(id);
    }

    @Override
    public JxcFinancePaymentRequestExt queryBySid(String sid) throws Exception {
        JxcFinancePaymentRequestExt requestExt = jxcFinancePaymentRequestMapper.selectBySid(sid);
        if (requestExt != null) {
            List<JxcFinancePaymentRequestDetailExt> detailExtList = jxcFinancePaymentRequestDetailService.queryValidByRequestList(Collections.singletonList(requestExt.getSid()));
            detailExtList = ListUtil.sorted(detailExtList, Comparator.comparing(JxcFinancePaymentRequestDetailExt::getSn));
            requestExt.setDetail_list(detailExtList);
        }
        return requestExt;
    }

    @Override
    public void deleteJxcFinancePaymentRequests(List<String> list, SysUser opUser) throws Exception {
        JxcFinancePaymentRequestExt params = new JxcFinancePaymentRequestExt();
        params.setDr(0);
        params.setSid_list(list);
        List<JxcFinancePaymentRequestExt> requestExtList = jxcFinancePaymentRequestMapper.selectByEntity(params);
        if (HlpUtils.isEmptyList(requestExtList)) {
            throw new HlpException("对账单为空");
        }
        GlobalSetting setting = systemSettingService.getGlobalSetting();
        ListUtil.forEach(requestExtList, t -> setting.procDisplay(t, GlobalSetting.TYPE_CLIENT));
        ListUtil.forEach(requestExtList, t -> setting.procDisplay(t, GlobalSetting.TYPE_SUPPLIER));
        this.checkLatestRecord(requestExtList, "删除");

        List<String> requestList = ListUtil.map(requestExtList, JxcFinancePaymentRequestExt::getSid);
        List<JxcFinancePaymentRequestDetailExt> detailExtList = jxcFinancePaymentRequestDetailMapper.selectByRequestList(requestList);

        List<JxcFinancePaymentRequestExt> updateList = new ArrayList<>();
        for (JxcFinancePaymentRequestExt detailExt : requestExtList) {
            super.autoInjectBaseData(detailExt, opUser, this.TYPE_DELETE);
            updateList.add(detailExt);
        }
        if (!HlpUtils.isEmptyList(updateList)) {
            ThreadPoolTaskUtil.executeInList4Split(updateList, false, itemList -> jxcFinancePaymentRequestMapper.updateList(itemList));
        }

        // 更新对账状态
        List<String> typeList = ListUtil.map(requestExtList, JxcFinancePaymentRequestExt::getType);
        for (String type : typeList) {
            List<JxcFinancePaymentRequestExt> filter = ListUtil.filter(requestExtList, t -> type.equals(t.getType()));
            if (!HlpUtils.isEmptyList(filter)) {
                List<String> filterReqList = ListUtil.map(filter, JxcFinancePaymentRequestExt::getSid);
                List<JxcFinancePaymentRequestDetailExt> filterDetail = ListUtil.filter(detailExtList, t -> filterReqList.contains(t.getRequest_sid()));
                if (!HlpUtils.isEmptyList(filterReqList)) {
                    List<String> srcTypeList = ListUtil.map(filterDetail, JxcFinancePaymentRequestDetailExt::getSrc_type);
                    for (String srcType : srcTypeList) {
                        this.updatePayReqState(filter, filterDetail, type, srcType);
                    }
                }
            }
        }
    }

    @Override
    public boolean isDuplicated(Object obj, String code) {
        if (obj == null) {
            return false;
        }
        JxcFinancePaymentRequestExt orderExt = (JxcFinancePaymentRequestExt) obj;
        orderExt.setCode(code);
        return jxcFinancePaymentRequestMapper.selectDuplicatedCode(orderExt) != null;
    }

    @Override
    public void saveJxcFinancePaymentRequest(JxcFinancePaymentRequestExt record, SysUser opUser, boolean isXd) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            record.setGroup_sid(opUser.getGroup_sid());
            super.checkEmpty(record.getType(), "请提供参数：type");
            if (FinanceConstants.TYPE_SALE.equals(record.getType())) {
                super.checkEmpty(record.getClient_sid(), "请提供参数：client_sid");
            } else {
                super.checkEmpty(record.getSupplier_sid(), "请提供参数：supplier_sid");
            }
            String ruleCode = FinanceConstants.TYPE_SALE.equals(record.getType()) ? JxcCodeRuleConstants.TYPE_JXC_FIN_RECONCILIATION_SALE : JxcCodeRuleConstants.TYPE_JXC_FIN_RECONCILIATION_BUY;
            String ruleName = FinanceConstants.TYPE_SALE.equals(record.getType()) ? JxcCodeRuleConstants.TYPE_NAME_JXC_FIN_RECONCILIATION_SALE : JxcCodeRuleConstants.TYPE_NAME_JXC_FIN_RECONCILIATION_BUY;
            if (isXd) {
                ruleCode = FinanceConstants.TYPE_SALE.equals(record.getType()) ? JxcCodeRuleConstants.TYPE_XD_FIN_RECONCILIATION_SALE : JxcCodeRuleConstants.TYPE_XD_FIN_RECONCILIATION_BUY;
                ruleName = FinanceConstants.TYPE_SALE.equals(record.getType()) ? JxcCodeRuleConstants.TYPE_NAME_XD_FIN_RECONCILIATION_SALE : JxcCodeRuleConstants.TYPE_NAME_XD_FIN_RECONCILIATION_BUY;
            }
            if (!HlpUtils.isEmpty(record.getCode())) {
                //如果是手动设置了编号，则检查是否有相同的，有的话，置空后重新生成
                Integer check = jxcFinancePaymentRequestMapper.selectDuplicatedCode(record);
                if (check != null) {
                    record.setCode(null);
                }
            }
            String partySid = FinanceConstants.TYPE_SALE.equals(record.getType()) ? record.getClient_sid() : record.getSupplier_sid();
            String genCode = baseCodeRuleService.genCode(partySid, ruleCode, ruleName, opUser, record.getCode(), record, this);
            record.setCode(genCode);

            JxcFinancePaymentRequestExt latestRecord = jxcFinancePaymentRequestMapper.selectValidLatestRecord(record);
            if (latestRecord != null) {
                if (Constants.VALUE_NO == latestRecord.getAudit_state()) {
                    throw new HlpException("存在未审核的账单周期，无法新增");
                }
                record.setBegin_date(HlpUtils.getNextDate(latestRecord.getEnd_date(), 1));
                record.setPre_end_amount(latestRecord.getEnd_amount());
            } else {
                if (HlpUtils.isEmpty(record.getPre_end_amount())) {
                    record.setPre_end_amount(BigDecimal.ZERO);
                }
                JxcSettingVo settingVo = JxcUtils.getJxcSettingVo();
                record.setSource(settingVo.getReconciliation_source());
                Date beginDate = jxcFinancePaymentRequestMapper.selectOldestDate(record);
                if (beginDate == null) {
                    throw new HlpException("当前对账周期没有数据");
                }
                record.setBegin_date(beginDate);
            }

            record.setAudit_state(Constants.VALUE_NO);
            record.setState(Constants.VALUE_YES);
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            jxcFinancePaymentRequestMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            jxcFinancePaymentRequestMapper.updateByPrimaryKeySelective(record);
        }

        jxcFinancePaymentRequestDetailService.saveJxcFinancePaymentRequestDetail(record, opUser);
    }

    @Override
    public List<JxcFinancePaymentRequestExt> findJxcFinancePaymentRequests(Map<String, Object> params, PageBean page) throws Exception {
        String type = HlpUtils.getString(params, "type");
        if (FinanceConstants.TYPE_SALE.equals(type)) {
            return jxcFinancePaymentRequestMapper.select4SaleByPage(params, page);
        }
        return jxcFinancePaymentRequestMapper.select4BuyByPage(params, page);
    }

    @Override
    public List<JxcFinancePaymentRequestExt> findJxcFinancePaymentRequests(Map<String, Object> params) throws Exception {
        return jxcFinancePaymentRequestMapper.selectByParams(params);
    }

    private void checkLatestRecord(List<JxcFinancePaymentRequestExt> requestExtList, String operate) throws Exception {
        if (HlpUtils.isEmptyList(requestExtList)) {
            return;
        }
        for (JxcFinancePaymentRequestExt requestExt : requestExtList) {
            requestExt.setAudit_state(Constants.VALUE_YES);
            JxcFinancePaymentRequestExt latestRecord = jxcFinancePaymentRequestMapper.selectValidLatestRecord(requestExt);
            if (latestRecord == null || latestRecord.getSid().equals(requestExt.getSid())) {
                continue;
            }
            throw new HlpException("对账单：" + requestExt.getCode() + " 不是"
                    + (FinanceConstants.TYPE_SALE.equals(requestExt.getType()) ? ("客户：" + requestExt.getClient_display()) : ("供应商：" + requestExt.getSupplier_display()))
                    + "的最新记录，无法" + operate);
        }
    }

    @Override
    public void changeJxcFinancePaymentRequestAuditState(List<String> sidList, Integer state, SysUserExt opUser) throws Exception {
        JxcFinancePaymentRequestExt params = new JxcFinancePaymentRequestExt();
        params.setDr(0);
        params.setSid_list(sidList);
        List<JxcFinancePaymentRequestExt> requestExtList = jxcFinancePaymentRequestMapper.selectByEntity(params);
        if (HlpUtils.isEmptyList(requestExtList)) {
            throw new HlpException("对账单为空");
        }
        GlobalSetting setting = systemSettingService.getGlobalSetting();
        ListUtil.forEach(requestExtList, t -> setting.procDisplay(t, GlobalSetting.TYPE_CLIENT));
        ListUtil.forEach(requestExtList, t -> setting.procDisplay(t, GlobalSetting.TYPE_SUPPLIER));

        List<JxcFinancePaymentRequestExt> filterList = ListUtil.filter(requestExtList, t -> Constants.VALUE_NO == t.getState());
        if (!HlpUtils.isEmptyList(filterList)) {
            List<String> codeList = ListUtil.map(filterList, JxcFinancePaymentRequestExt::getCode);
            throw new HlpException("对账单：" + StringUtils.join(codeList, "、") + "已作废，无法" + (Constants.VALUE_YES == state ? "审核" : "反审"));
        }

        if (state == -1) {
            this.checkLatestRecord(requestExtList, "反审");
        }

        List<JxcFinancePaymentRequestExt> updateList = new ArrayList<>();
        for (JxcFinancePaymentRequestExt detailExt : requestExtList) {
            if (state == 1) {
                detailExt.setAudit_state(Constants.VALUE_YES);
                detailExt.setAuditor_sid(opUser.getSid());
                detailExt.setAuditor_name(opUser.getName());
                detailExt.setAudit_date(new Date());
            } else if (state == -1) {
                detailExt.setAudit_state(Constants.VALUE_NO);
                detailExt.setAuditor_sid(null);
                detailExt.setAuditor_name(null);
                detailExt.setAudit_date(null);
            }
            updateList.add(detailExt);
        }
        if (!HlpUtils.isEmptyList(updateList)) {
            ThreadPoolTaskUtil.executeInList4Split(updateList, false, itemList -> jxcFinancePaymentRequestMapper.updateList(itemList));
        }
    }

    @Override
    public List<JxcFinancePaymentRequestExt> cancelJxcFinancePaymentRequest(List<String> sidList, SysUserExt opUser) throws Exception {
        JxcFinancePaymentRequestExt params = new JxcFinancePaymentRequestExt();
        params.setDr(0);
        params.setSid_list(sidList);
        List<JxcFinancePaymentRequestExt> requestExtList = jxcFinancePaymentRequestMapper.selectByEntity(params);
        if (HlpUtils.isEmptyList(requestExtList)) {
            throw new HlpException("对账单为空");
        }
        GlobalSetting setting = systemSettingService.getGlobalSetting();
        ListUtil.forEach(requestExtList, t -> setting.procDisplay(t, GlobalSetting.TYPE_CLIENT));
        ListUtil.forEach(requestExtList, t -> setting.procDisplay(t, GlobalSetting.TYPE_SUPPLIER));

        List<JxcFinancePaymentRequestExt> filterList = ListUtil.filter(requestExtList, t -> Constants.VALUE_NO == t.getState());
        if (!HlpUtils.isEmptyList(filterList)) {
            List<String> codeList = ListUtil.map(filterList, JxcFinancePaymentRequestExt::getCode);
            throw new HlpException("对账单：" + StringUtils.join(codeList, "、") + "已作废");
        }

        this.checkLatestRecord(requestExtList, "作废");

        List<String> requestList = ListUtil.map(requestExtList, JxcFinancePaymentRequestExt::getSid);
        List<JxcFinancePaymentRequestDetailExt> detailExtList = jxcFinancePaymentRequestDetailMapper.selectByRequestList(requestList);

        List<JxcFinancePaymentRequestExt> updateList = new ArrayList<>();
        for (JxcFinancePaymentRequestExt detailExt : requestExtList) {
            detailExt.setState(Constants.VALUE_NO);
            super.autoInjectBaseData(detailExt, opUser, this.TYPE_UPDATE);
            updateList.add(detailExt);
        }
        if (!HlpUtils.isEmptyList(updateList)) {
            ThreadPoolTaskUtil.executeInList4Split(updateList, false, itemList -> jxcFinancePaymentRequestMapper.updateList(itemList));
        }

        // 更新对账状态
        List<String> typeList = ListUtil.map(requestExtList, JxcFinancePaymentRequestExt::getType);
        for (String type : typeList) {
            List<JxcFinancePaymentRequestExt> filter = ListUtil.filter(requestExtList, t -> type.equals(t.getType()));
            if (!HlpUtils.isEmptyList(filter)) {
                List<String> filterReqList = ListUtil.map(filter, JxcFinancePaymentRequestExt::getSid);
                List<JxcFinancePaymentRequestDetailExt> filterDetail = ListUtil.filter(detailExtList, t -> filterReqList.contains(t.getRequest_sid()));
                if (!HlpUtils.isEmptyList(filterReqList)) {
                    List<String> srcTypeList = ListUtil.map(filterDetail, JxcFinancePaymentRequestDetailExt::getSrc_type);
                    for (String srcType : srcTypeList) {
                        this.updatePayReqState(filter, filterDetail, type, srcType);
                    }
                }
            }
        }
        return requestExtList;
    }

    public void updatePayReqState(List<JxcFinancePaymentRequestExt> requestExtList, List<JxcFinancePaymentRequestDetailExt> detailExtList, String type, String srcType) {
        List<JxcFinancePaymentRequestExt> filterReqList = ListUtil.filter(requestExtList, t -> type.equals(t.getType()));
        if (HlpUtils.isEmptyList(filterReqList)) {
            return;
        }
        List<String> reqList = ListUtil.map(filterReqList, JxcFinancePaymentRequestExt::getSid);
        List<JxcFinancePaymentRequestDetailExt> filterList = ListUtil.filter(detailExtList, t -> reqList.contains(t.getRequest_sid()) && srcType.equals(t.getSrc_type()));
        if (HlpUtils.isEmptyList(filterList)) {
            return;
        }
        List<String> orderDetailList = ListUtil.map(filterList, JxcFinancePaymentRequestDetailExt::getSrc_sid);
        if (JxcFinancePaymentRequestDetailExt.SRC_TYPE_FUND.equals(srcType)) {
            ThreadPoolTaskUtil.executeInList4Split(orderDetailList, false, itemList -> jxcFinancePaymentRequestDetailMapper.updatePayReqState4FundDetail(itemList, Constants.VALUE_NO));
        } else if (JxcFinancePaymentRequestDetailExt.SRC_TYPE_CUT.equals(srcType)) {
            ThreadPoolTaskUtil.executeInList4Split(orderDetailList, false, itemList -> jxcFinancePaymentRequestDetailMapper.updatePayReqState4CutMoney(itemList, Constants.VALUE_NO));
        } else {
            ThreadPoolTaskUtil.executeInList4Split(orderDetailList, false, itemList -> {
                List<String> parentList = ListUtil.filterMap(filterList, t -> itemList.contains(t.getSrc_sid()), JxcFinancePaymentRequestDetailExt::getSrc_parent_sid);
                if (JxcFinancePaymentRequestDetailExt.SRC_TYPE_ORDER.equals(srcType)) {
                    if (FinanceConstants.TYPE_SALE.equals(type)) {
                        jxcFinancePaymentRequestDetailMapper.updateSalePayReqStateBySaleOrderDetailList(itemList, Constants.VALUE_NO);
                        jxcFinancePaymentRequestDetailMapper.updateSalePayReqStateBySaleOrderList(parentList);
                    } else {
                        jxcFinancePaymentRequestDetailMapper.updateBuyPayReqStateByBuyOrderDetailList(itemList, Constants.VALUE_NO);
                        jxcFinancePaymentRequestDetailMapper.updateBuyPayReqStateByBuyOrderList(parentList);
                    }
                } else if (JxcFinancePaymentRequestDetailExt.SRC_TYPE_IN_BILL.equals(srcType)) {
                    jxcFinancePaymentRequestDetailMapper.updateSendPayReqStateByInBillItemList(itemList, Constants.VALUE_NO);
                    jxcFinancePaymentRequestDetailMapper.updateSendPayReqStateByInBillList(parentList);
                } else if (JxcFinancePaymentRequestDetailExt.SRC_TYPE_OUT_BILL.equals(srcType)) {
                    jxcFinancePaymentRequestDetailMapper.updateSendPayReqStateByOutBillItemList(itemList, Constants.VALUE_NO);
                    jxcFinancePaymentRequestDetailMapper.updateSendPayReqStateByOutBillList(parentList);
                } else {
                    jxcFinancePaymentRequestDetailMapper.updateSendPayReqStateBySendDetailList(itemList, type, Constants.VALUE_NO);
                    jxcFinancePaymentRequestDetailMapper.updateSendPayReqStateBySendList(parentList, type);
                }
            });
        }
    }

    @Override
    public JxcFinancePaymentRequestExt getInitRequestFormData(Map<String, Object> map) throws Exception {
        String type = HlpUtils.getString(map, "type");
        super.checkEmpty(type, "缺少参数：type");
        JxcFinancePaymentRequestExt params = new JxcFinancePaymentRequestExt();
        params.setAudit_state(Constants.VALUE_YES);
        if (Arrays.asList(FinanceConstants.TYPE_SALE, FinanceConstants.TYPE_PCB_SALE).contains(type)) {
            params.setType(FinanceConstants.TYPE_SALE);
            String clientSid = HlpUtils.getString(map, "client_sid");
            super.checkEmpty(clientSid, "缺少参数：client_sid");
            params.setClient_sid(clientSid);
        } else {
            params.setType(FinanceConstants.TYPE_BUY);
            String supplierSid = HlpUtils.getString(map, "supplier_sid");
            super.checkEmpty(supplierSid, "缺少参数：supplier_sid");
            params.setSupplier_sid(supplierSid);
        }
        JxcFinancePaymentRequestExt requestExt = new JxcFinancePaymentRequestExt();
        requestExt.setPre_end_amount(BigDecimal.ZERO);
        requestExt.setAmount(BigDecimal.ZERO);
        requestExt.setPayment_amount(BigDecimal.ZERO);
        requestExt.setEnd_amount(BigDecimal.ZERO);
        JxcFinancePaymentRequestExt latestRecord = jxcFinancePaymentRequestMapper.selectValidLatestRecord(params);
        if (latestRecord != null) {
            requestExt.setPre_end_amount(latestRecord.getEnd_amount());
            requestExt.setEnd_amount(latestRecord.getEnd_amount());
        }
        return requestExt;
    }

    private List<JxcFinancePaymentRequestDetailExt> queryData4Order(Map<String, Object> params) throws Exception {
        List<JxcFinancePaymentRequestDetailExt> list = new ArrayList<>();
        String type = HlpUtils.getString(params, "type");
        String cutType = HlpUtils.getString(params, "type");
        if (FinanceConstants.TYPE_PCB_SALE.equals(type)) {
            list = jxcFinancePaymentRequestDetailMapper.selectSaleDataBySend(params);
            cutType = FinanceConstants.TYPE_SALE;
        } else if (FinanceConstants.TYPE_PCB_BUY.equals(type)) {
            list = jxcFinancePaymentRequestDetailMapper.selectBuyDataBySend(params);
            cutType = FinanceConstants.TYPE_BUY;
        } else if (FinanceConstants.TYPE_SALE.equals(type)) {
            JxcSettingVo settingVo = JxcUtils.getJxcSettingVo();
            if (JxcSettingVo.RECONCILIATION_SOURCE_ORDER.equals(settingVo.getReconciliation_source())) {
                list = jxcFinancePaymentRequestDetailMapper.selectSaleDataByOrder(params);
            } else if (JxcSettingVo.RECONCILIATION_SOURCE_SEND.equals(settingVo.getReconciliation_source())) {
                list = jxcFinancePaymentRequestDetailMapper.selectSaleDataBySend(params);
            } else if (JxcSettingVo.RECONCILIATION_SOURCE_STOCK_BILL.equals(settingVo.getReconciliation_source())) {
                List<JxcFinancePaymentRequestDetailExt> inBillList = jxcFinancePaymentRequestDetailMapper.selectSaleDataByStockInBill(params);
                if (!HlpUtils.isEmptyList(inBillList)) {
                    list.addAll(inBillList);
                }
                List<JxcFinancePaymentRequestDetailExt> outBillList = jxcFinancePaymentRequestDetailMapper.selectSaleDataByStockOutBill(params);
                if (!HlpUtils.isEmptyList(outBillList)) {
                    list.addAll(outBillList);
                }
            }
        } else if (FinanceConstants.TYPE_BUY.equals(type)) {
            JxcSettingVo settingVo = JxcUtils.getJxcSettingVo();
            if (JxcSettingVo.RECONCILIATION_SOURCE_ORDER.equals(settingVo.getReconciliation_source())) {
                list = jxcFinancePaymentRequestDetailMapper.selectBuyDataByOrder(params);
            } else if (JxcSettingVo.RECONCILIATION_SOURCE_SEND.equals(settingVo.getReconciliation_source())) {
                list = jxcFinancePaymentRequestDetailMapper.selectBuyDataBySend(params);
            } else if (JxcSettingVo.RECONCILIATION_SOURCE_STOCK_BILL.equals(settingVo.getReconciliation_source())) {
                List<JxcFinancePaymentRequestDetailExt> inBillList = jxcFinancePaymentRequestDetailMapper.selectBuyDataByStockInBill(params);
                if (!HlpUtils.isEmptyList(inBillList)) {
                    list.addAll(inBillList);
                }
                List<JxcFinancePaymentRequestDetailExt> outBillList = jxcFinancePaymentRequestDetailMapper.selectBuyDataByStockOutBill(params);
                if (!HlpUtils.isEmptyList(outBillList)) {
                    list.addAll(outBillList);
                }
            }
        }
        if (!HlpUtils.isEmptyList(list)) {
            String requestSid = HlpUtils.getString(params, "request_sid");
            List<String> detailList = ListUtil.map(list, JxcFinancePaymentRequestDetailExt::getOrder_detail_sid);
            List<JxcOrderCutMoneyExt> moneyExtList = jxcOrderCutMoneyMapper.selectByOrderDetailList(cutType, detailList, requestSid);
            if (!HlpUtils.isEmptyList(moneyExtList)) {
                for (JxcFinancePaymentRequestDetailExt detailExt : list) {
                    List<JxcOrderCutMoneyExt> filter = ListUtil.filter(moneyExtList, t -> t.getOrder_detail_sid().equals(detailExt.getOrder_detail_sid()));
                    filter = ListUtil.sorted(filter, Comparator.comparing(JxcOrderCutMoneyExt::getId));
                    detailExt.setCut_money_list(filter);
                }
            }
        }
        return list;
    }

    @Override
    public List<JxcFinancePaymentRequestDetailExt> getJxcFinancePaymentRequestDetail4Select(Map<String, Object> params) throws Exception {
        List<JxcFinancePaymentRequestDetailExt> orderList = this.queryData4Order(params);
        String baseType = HlpUtils.getString(params, "type");
        if (Arrays.asList(FinanceConstants.TYPE_PCB_SALE, FinanceConstants.TYPE_PCB_BUY).contains(baseType)) {
            params.put("type", FinanceConstants.TYPE_PCB_SALE.equals(baseType) ? FinanceConstants.TYPE_SALE : FinanceConstants.TYPE_BUY);
        }
        List<JxcFinancePaymentRequestDetailExt> fundList = jxcFinancePaymentRequestDetailMapper.selectData4Fund(params);
        if (!HlpUtils.isEmptyList(fundList)) {
            orderList.addAll(fundList);
        }
        orderList = ListUtil.sorted(orderList, Comparator.comparing(JxcFinancePaymentRequestDetailExt::getCreate_date)
                .thenComparing(JxcFinancePaymentRequestDetailExt::getSn));
        return orderList;
    }

    @Override
    public PaymentRequestExportVo getRequestExportData(Map<String, Object> map, SysUserExt opUser) throws Exception {
        String sid = HlpUtils.getString(map, "sid");
        super.checkEmpty(sid, "缺少参数：sid");
        JxcFinancePaymentRequestExt requestExt = this.queryBySid(sid);
        PaymentRequestExportVo exportVo = requestExt.convertExportVo();
        SysSettingExt params = new SysSettingExt();
        params.setCode(opUser.getSid());
        SysSettingExt settingExt = sysSettingService.getSysSettingByCodeOwnerSid(params);
        if (settingExt != null && !HlpUtils.isEmpty(settingExt.getJson())) {
            PaymentRequestExportVo vo = JSON.parseObject(settingExt.getJson(), PaymentRequestExportVo.class);
            exportVo.setCompany_name(vo.getCompany_name());
            exportVo.setBank_account(vo.getBank_account());
            exportVo.setBank(vo.getBank());
            exportVo.setLinker_name(vo.getLinker_name());
            exportVo.setLinker_phone(vo.getLinker_phone());
            exportVo.setLinker_address(vo.getLinker_address());
            exportVo.setThe_date(vo.getThe_date());
        }
        return exportVo;
    }
}